window.onload=function (){
    if (localStorage.getItem('userHao') == null) {
        window.location = 'http://localhost:8080/future/loginpage';
    }
    var judge = true;
    var music = document.getElementsByClassName('music')[0];
    var musicForm = document.getElementsByClassName('musicForm')[0];
    music.onclick = function () {
        if (judge == true) {
            musicForm.style.display = 'block';
            judge = false;
            console.log();
        } else {
            musicForm.style.display = 'none';
            judge = true;
        }
    }

    // 查询专辑
    var input = document.getElementsByClassName('input')[0];//用户名的输入框
    var ssBtn = document.getElementsByClassName('ssBtn')[0];//搜索的按钮
    var Apage=document.getElementsByClassName('pageAll')[0];

    ssBtn.onclick = function () {
        $.ajax({
            url: 'http://localhost:8080/future/admin/queryAllAlbum',
            method: "GET",
            data: {//参数
                albName: input.value,
            },
            success: function (data) {
                console.log(data);
                if(data.length===0){
                    console.log(data);
                    alert("查无此信息！");
                    location.reload();
                }
                else{
                document.getElementsByClassName('content')[0].innerHTML = '';
                    console.log(data.length);
                    var a=data.length;
                    Math.ceil(a / 10); //向上整除 4/3=2;
                    console.log(  Math.ceil(a / 10));
                for (var index = 0; index < data.length; index++) {
                    var element=data[index];
                    var i = index + 1;
                    document.getElementsByClassName("content")[0].innerHTML += "<tr class='found'>"+ "<td class='find'>" + "<input class='choice' type='checkbox' name='ids'>" + "</td>"  + "<td class='find'>" + i + "</td>" + "<td class='find'>" + element.albName + "</td>" + "<td class='find'>" + element.albThing + "</td>" + "<td class='find'>" + element.albHot + "</td>" + "<td class='find'>" + element.albTime + "</td>" +
                        "<td class='find'>" + element.albType + "</td>" + "<td class='find'>" + element.albLanguage + "</td>" +
                        "<td class='find'>" + "<button class='del'>删除</button>" + "<button class='amend'>更改</button>" + "</td>" + "</tr>"
                }
                    Apage.innerHTML=  Math.ceil(a / 10);
                }
            },
            error: function (err) {//请求失败，执行的回调函数

                console.log('错误');
            }
        })
    }

// 新增的弹窗
    var addUser = document.getElementsByClassName('addUser')[0];
    var add = document.getElementsByClassName('add')[0];
    var affirm = document.getElementsByClassName('affirm')[0];
    var cancel = document.getElementsByClassName('cancel')[0];
    var reset = document.getElementsByClassName('reset')[0];
    add.onclick = function () {
        addUser.style.display = 'block';
    }
    affirm.onclick = function () {
        addUser.style.display = 'none';
    }
    cancel.onclick = function () {
        addUser.style.display = 'none';
    }
    reset.onclick = function () {
        window.location.href = "http://localhost:8080/future/admin/queryAllAlbum";//页面刷新
    }

    //限制日期的输入格式
    var DATA=document.getElementById('Data');
    DATA.onblur=function (){
    var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
    var str1 =DATA.value;
    if (str1 == "") {
        alert("请输入日期");
    }
    else if(!reg.test(str1) && RegExp.$2 <= 12 && RegExp.$3 <= 31) {
        alert("请保证输入的为yyyy-mm-dd日期格式");
    }
    }

    //新增一张专辑
    var information=document.getElementsByClassName('information');
    var selectType=document.getElementsByClassName('selectType')[0];
    affirm.onclick = function () {

        $.ajax({
            url: 'http://localhost:8080/future/admin/addAlbum',
            method: 'POST',
            data: {
                albName:information[0].value,
                albThing:information[1].value,
                userId:information[2].value,
                albHot:information[3].value,
                albImgload:information[4].value,
                albTime:information[5].value,
                albLanguage:information[6].value,
                albType:selectType.value,
            },
            success: function (data) {

                console.log(data);//控制台显示返回的数据
                location.reload();//添加成功则进行页面刷新

            },
            error: function (err) {//请求失败，执行的回调函数

                console.log('错误');
            }

        })
    }

    //删除一张专辑

    var del = document.getElementsByClassName('del');
    for (var i = 0; i < del.length; i++) {
        del[i].onclick = function () {
            console.log(this.parentNode.parentNode.getAttribute("everid"));//获取当前行的id

            var flag = confirm("是否要删除该专辑", "确定");
            if (flag) {

                $.ajax({
                    url: "http://localhost:8080/future/music/deleteAlbum",
                    type: "POST",
                    data: {
                        albId: this.parentNode.parentNode.getAttribute("everid"),
                    },
                    success: function (data) {
                        console.log(data);
                        if (data.statusCode === 1) {
                            alert("删除成功");
                            window.location.reload();
                        } else {
                            alert("删除失败");
                            console.log(data.info);
                        }


                    },
                    error: function (err) {
                        console.log("错误");
                    },
                })
            }
        }
    }


    //修改专辑信息

    var albName=document.getElementsByClassName('albName')[0];
    var albIntroduce=document.getElementsByClassName('albIntroduce')[0];
    var usId=document.getElementsByClassName('usId')[0];
    var albHot=document.getElementsByClassName('albHot')[0];
    var albImg=document.getElementsByClassName('albImg')[0];
    var albTime=document.getElementsByClassName('albTime')[0];
    var albLanguage=document.getElementsByClassName('albLanguage')[0];
    var selecttype=document.getElementsByClassName('selecttype')[0];
    var change=document.getElementsByClassName('amendUser')[0];
    var affirm_2=document.getElementsByClassName('affirm_2')[0];
    var cancel_2=document.getElementsByClassName('cancel_2')[0];
    var amend=document.getElementsByClassName('amend');
    $('.content').on('click', '.amend', function () {
        console.log(this.parentNode.parentNode.getAttribute("everid"));//获取当前行的id
        change.style.display = 'block';
        albName.value=this.parentNode.parentNode.children[2].innerHTML;
        albIntroduce.value=this.parentNode.parentNode.children[3].innerHTML;
        albHot.value=this.parentNode.parentNode.children[4].innerHTML;
        albTime.value=this.parentNode.parentNode.children[5].innerHTML;
        selecttype.value=this.parentNode.parentNode.children[6].innerHTML;
        albLanguage.value=this.parentNode.parentNode.children[7].innerHTML;
        albImg.value=this.parentNode.parentNode.getAttribute("albImgload");
        // usId.value=this.parentNode.parentNode.getAttribute("userId");
        var albId=this.parentNode.parentNode.getAttribute("everId");
        affirm_2.onclick = function () {
            var flag=confirm("是否确认要修改当前用户的信息","确认");
            if(flag){
                alter();

            }else {
                change.style.display = 'none';
            }

        }
        cancel_2.onclick = function () {
            change.style.display = 'none';
        }

        function alter() {
            $.ajax({
                url: 'http://localhost:8080/future/admin/updateAlbum',
                method: 'GET',
                data: {
                    albName:albName.value,
                    albThing:albIntroduce.value,
                    albHot:albHot.value,
                    albImgload:albImg.value,
                    albTime:albTime.value,
                    albLanguage:albLanguage.value,
                    albType:selecttype.value,
                    albId:albId,
                },
                dataType: "json",
                success: function (data) {
                    location.reload();
                    console.log(data);
                },
                error: function (err) {//请求失败，执行的回调函数
                    console.log('错误');
                }
            })
        }
    })


    //头像处的列表的出现
    var headPhoto = document.getElementsByClassName('headPhoto')[0];
    var personage = document.getElementsByClassName('personage')[0];
    var xia2 = document.getElementsByClassName('xia2')[0];
    headPhoto.innerHTML = localStorage.getItem('userHao');
    headPhoto.onclick = function () {
        if (judge == true) {
            console.log("aaaaa");
            personage.style.height = '8%';
            judge = false;
        } else {
            personage.style.height = '0%';
            judge = true;
        }

    }


    //退出登录
    var back = document.getElementsByClassName('back')[0];
    back.onclick = function () {
        var flag = confirm("是否确定要退出登录", "确定");
        if (flag) {
            window.location.href = 'http://localhost:8080/future/loginpage';
            localStorage.removeItem('userHao');//清除保留的用户名
        }

    }


    //全选
    //点击获取下面全部的信息，再点取消
    var all=document.getElementById('all');
    function DoCheck()
    {
        var ch=document.getElementsByName("ids");
        if(document.getElementsByName("allChecked")[0].checked==true) {
            for(var i=0;i<ch.length;i++) {
                ch[i].checked=true;
            }
        }else{
            for(var i=0;i<ch.length;i++)
            {
                ch[i].checked=false;
            }
        }
    }
    all.onclick=DoCheck;





    //分页查询
    var previous=document.getElementsByClassName('previous')[0];
    var next=document.getElementsByClassName('next')[0];
    var page=1;
    var toPage=document.getElementsByClassName('toPage')[0];
    var pageAll=document.getElementsByClassName('pageAll')[0].innerHTML
    var first=document.getElementsByClassName('first')[0];
    var end=document.getElementsByClassName('end')[0];
    first.onclick=function () {
        location.href = 'http://localhost:8080/future/admin/queryAllAlbum?page=1';
    }
    end.onclick=function (){
        location.href = 'http://localhost:8080/future/admin/queryAllAlbum?page='+pageAll;
    }
    previous.onclick=function(){
        if(toPage.value<=1){
            alert("这已经是最前面一页！");
            return;
        }else{
            page=toPage.value-1;
            location.href = 'http://localhost:8080/future/admin/queryAllAlbum?page='+page;
        }

    }
    next.onclick=function(){
        console.log(pageAll);
        var pagenum = pageAll;
        if(Number(toPage.value)>=pagenum){
            alert("没有下一页了！");
            return;
        }else{
            page=Number(toPage.value)+1;

            location.href = 'http://localhost:8080/future/admin/queryAllAlbum?page='+page;
            console.log(page);
        }
    }

    //更改页码框中的值
    toPage.onkeydown=function(event) {
        var event = event;
        // console.log(event.keyCode)
        if (event.keyCode == 13) {
            location.href = 'http://localhost:8080/future/admin/queryAllAlbum?page=' + Number(toPage.value);
        }
    }

//批量删除
    $(function(){
        $(".batch").click(function(){
            //判断是否至少选择一项
            var checkedNum = $("input[name='ids']:checked").length;
            console.log(checkedNum);
            if(checkedNum==0){
                alert("请至少选择一项进行删除");
                return;
            }
            if(confirm("确定要删除所选项目？")){
                //这个数组存储要删除的数据的id
                var checkedList = [];
                //每一个被选中的name为ids的input标签
                $("input[name='ids']:checked").each(function(){
                    checkedList.push(Number($(this).val()));//将id存入数组
                    console.log(checkedList);

                });
                $.ajax({
                    type:"POST",
                    dataType:"json",
                    // contentType:"application/json",
                    url:"http://localhost:8080/future/admin/BatchdeleteUsers",//不同页面只用更改action即可
                    async:true,
                    //数组的toString里的id是用","分隔开的，例如数组{"1","2","3"},同String后为"1,2,3"
                    data:{
                        ids:JSON.stringify(checkedList),
                    },
                    success:function(data){
                        console.log(data)
                        // if(data==1){
                        //     for(var i=0;i<checkedList.length;i++){
                        //         $("#tr_"+checkedList[i]).remove();//ajax动态移除
                        //     }
                        //     alert("删除成功");
                        // }else{
                        //     alert("删除失败");
                        // }
                    },
                    error:function(){
                        alert("错误");
                    }
                })
            }
        })

    })




















}